<?php

abstract class Soul_Template_Html_Tag {

	protected $id = NULL;
	protected $templateName = NULL;
	protected $enabled = TRUE;
	protected $attributes = array();
	protected $styles = array();
	protected $classes = array();
	protected $settings = array();
	protected $events = array();
	
	public function __toString(){
		return $this->toHtml();
	}

	public function getAttributes() {
		return $this->attributes;
	}

	public function getAttribute($name) {
		if (isset($this->attributes[$name])) {
			return $this->attributes[$name];
		}
		return '';
	}

	public function setAttribute($name, $value) {
		$this->attributes[$name] = $value;
		return $this;
	}

	public function getSettings() {
		return $this->settings;
	}

	public function getSetting($name) {
		return $this->settings[$name];
	}

	public function setSetting($name, $value) {
		$this->settings[$name] = $value;
		return $this;
	}

	public function bindEvent($event, $script) {
		$this->events[$event] = $script;
		return $this;
	}

	public function unbindEvent($event) {
		if (isset($this->events[$event])) {
			unsset($this->events[$event]);
		}
		return $this;
	}

	public function getClasses() {
		return $this->classes;
	}

	public function setClasses() {
		$this->classes = func_get_args();
		return $this;
	}

	public function addClass() {
		$arr = func_get_args();
		$this->classes = array_merge($this->classes, $arr);
		return $this;
	}

	public function getStyles() {
		return $this->styles;
	}

	public function setStyle($name, $value) {
		$this->styles[$name] = $value;
		return $this;
	}

	public function getStyle($name) {
		if(isset($this->styles[$name])){
			return $this->styles[$name];
		}
		return NULL;
	}

	public function getId() {
		return $this->getAttribute('id');
	}

	public function setId($value) {
		return $this->setAttribute('id', $value);
	}

	public function getEnabled() {
		return $this->enabled;
	}

	public function setEnabled($value) {
		$this->enabled = $value;
		return $this;
	}

	public function getAttributeHtml() {
		$atts = array();
		foreach ($this->getAttributes() as $k => $v) {
			if(!empty($v)){
				$atts[] = $k . '="' . $v . '"';
			}
		}

		$style = $this->getStyleHtml();
		if (!empty($style)) {
			$atts[] = $style;
		}

		$setting = $this->getSettingHtml();
		if (!empty($setting)) {
			$atts[] = $setting;
		}

		$events = $this->getEventHtml();
		if (!empty($events)) {
			$atts[] = $events;
		}

		$class = $this->getClassHtml();
		if (!empty($class)) {
			$atts[] = $class;
		}

		if (!empty($atts)) {
			return ' ' . implode(' ', $atts);
		}
		return '';
	}

	public function getStyleHtml() {
		$atts = array();
		if (!empty($this->styles)) {
			foreach ($this->getStyles() as $k => $v) {
				$atts[] = "$k:$v";
			}
		}
		if (!empty($atts)) {
			return 'style="' . implode(';', $atts) . '"';
		}
		return '';
	}

	public function getClassHtml() {
		if ($this->getEnabled() != TRUE) {
			$this->addClass('disabled');
		}
		$result = '';
		if (!empty($this->classes)) {
			$result = 'class="' . implode(' ', array_unique($this->classes)) . '"';
		}
		return $result;
	}

	public function getSettingHtml() {
		if (!empty($this->settings)) {
			return 'setting="' . Soul_Util_Json::encode($this->getSettings(), TRUE) . '"';
		}
		return '';
	}

	public function getEventHtml() {
		$result = '';
		if (!empty($this->events)) {
			$result = array();
			foreach ($this->events as $event => $script) {
				$result[] = "on$event=\"javascript:$script\"";
			}
			$result = implode(' ', $result);
		}
		return $result;
	}

	public function getType() {
		$classNames = explode('_', get_class($this));
		return strtolower(end($classNames));
	}

	public function toHtml() {
		if (!empty($this->templateName)) {
			if(count(Soul_Config::$templateDirectories)> 0){
				$file = Soul_Util_FS::findFile(Soul_Config::$templateDirectories, $this->templateName.'.phtml');
				if(!is_null($file)){
					ob_start();
					include $file;
					return ob_get_clean();
				}
			}
		}
		return $this->render();
	}

	abstract protected function render();
}